package com.hankol.community

import android.text.TextUtils
import android.view.inputmethod.EditorInfo
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.MessageBus
import com.baselibrary.activity.BaseActivity
import com.baselibrary.fragment.CommonDynamicFragment
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils.parseParams
import com.hankol.community.databinding.ActivityTopicSearchBinding
import com.baselibrary.utils.SpanUtils
import com.baselibrary.utils.ToastUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 *@author:hanke
 *2022/4/6
 *desc:话题搜索页
 */
@Route(path = ActivityRouter.COMMUNITY.COMMUNITY_TOPIC_SEARCH)
class TopicSearchActivity : BaseActivity() {
    private var contentDetail = ""
    private var topicId = ""
    private lateinit var mDataBinding: ActivityTopicSearchBinding
    private lateinit var commonDynamicFragment: CommonDynamicFragment
    override fun getLayoutId(): Int {
        return R.layout.activity_topic_search
    }

    override fun init() {
        setStatusBarHeight(R.color.color_FFFFFF)
        mDataBinding = getDataBinding<ActivityTopicSearchBinding>()
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        initWebView()
    }

    private fun initWebView() {
        intent.extras?.let {
            contentDetail = parseParams(intent.extras!!, "tName")[0]
            topicId = parseParams(intent.extras!!, "tid")[0]
        }
        mDataBinding.tvTopicName.text = "在 #${SpanUtils.appendEllipsis(contentDetail)}中搜索"
        commonDynamicFragment = CommonDynamicFragment.newInstance(CommonDynamicFragment.DYNAMIC_TYPE_TOPIC_SEARCH, topicId)
        mDataBinding.tvTopicName.postDelayed(Runnable {
            //需要加延迟，否则不生效
            commonDynamicFragment.setDefaultView(contentDetail)
        }, 500)
        supportFragmentManager
                .beginTransaction()
                .add(R.id.fl_container, commonDynamicFragment)
                .commit()

        mDataBinding.ivTopicSearchBack.setOnClickListener { finish() }
        mDataBinding.tvCancelSearch.setOnClickListener { finish() }
        mDataBinding.edtSearchTopic.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                if (TextUtils.isEmpty(mDataBinding.edtSearchTopic.text.toString().trim())) {
                    ToastUtils.showShort(this, "请输入搜索关键字")
                } else {
                    commonDynamicFragment.searchKeywords(mDataBinding.edtSearchTopic.text.toString())
                }
                SensorsDataManager.instance.doSearch("话题", mDataBinding.edtSearchTopic.text.toString())
            }
            false
        }
    }

    //EventBus 接受方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(messageBus: MessageBus) {
        if (messageBus.codeType == MessageBus.msgId_load_dynamic_stream_is_have_data) {
            val isHaveData = messageBus.param1 as Boolean
            if (isHaveData) {
                //有数据 显示话题名称
                mDataBinding.tvTopicName.text = "#$contentDetail"
            } else {//无数据显示 在xxx内搜索
                mDataBinding.tvTopicName.text = "在 #${SpanUtils.appendEllipsis(contentDetail)}内搜索"
            }
        }
    }

    override fun onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
        super.onDestroy()
    }
}